{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# Quick Start Example - Extracting anomalies from a document, with source references and justifications"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Quick Start Example - Extracting anomalies from a document, with source references and justifications\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, StringConcept\n",
        "\n",
        "# Sample document text (shortened for brevity)\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"Consultancy Agreement\\n\"\n",
        "        \"This agreement between Company A (Supplier) and Company B (Customer)...\\n\"\n",
        "        \"The term of the agreement is 1 year from the Effective Date...\\n\"\n",
        "        \"The Supplier shall provide consultancy services as described in Annex 2...\\n\"\n",
        "        \"The Customer shall pay the Supplier within 30 calendar days of receiving an invoice...\\n\"\n",
        "        \"The purple elephant danced gracefully on the moon while eating ice cream.\\n\"  # \ud83d\udc8e anomaly\n",
        "        \"This agreement is governed by the laws of Norway...\\n\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Attach a document-level concept\n",
        "doc.concepts = [\n",
        "    StringConcept(\n",
        "        name=\"Anomalies\",  # in longer contexts, this concept is hard to capture with RAG\n",
        "        description=\"Anomalies in the document\",\n",
        "        add_references=True,\n",
        "        reference_depth=\"sentences\",\n",
        "        add_justifications=True,\n",
        "        justification_depth=\"brief\",\n",
        "        # see the docs for more configuration options\n",
        "    )\n",
        "    # add more concepts to the document, if needed\n",
        "    # see the docs for available concepts: StringConcept, JsonObjectConcept, etc.\n",
        "]\n",
        "# Or use `doc.add_concepts([...])`\n",
        "\n",
        "# Define an LLM for extracting information from the document\n",
        "llm = DocumentLLM(\n",
        "    model=\"openai/gpt-4o-mini\",  # or another provider/LLM\n",
        "    api_key=os.environ.get(\n",
        "        \"CONTEXTGEM_OPENAI_API_KEY\"\n",
        "    ),  # your API key for the LLM provider\n",
        "    # see the docs for more configuration options\n",
        ")\n",
        "\n",
        "# Extract information from the document\n",
        "doc = llm.extract_all(doc)  # or use async version `await llm.extract_all_async(doc)`\n",
        "\n",
        "# Access extracted information in the document object\n",
        "print(\n",
        "    doc.concepts[0].extracted_items\n",
        ")  # extracted items with references & justifications\n",
        "# or `doc.get_concept_by_name(\"Anomalies\").extracted_items`\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}